//
//  ARDetailCellTableViewCell.m
//  BrushFace
//
//  Created by timer_q on 16/1/17.
//  Copyright © 2016年 Zzz. All rights reserved.
//

#import "ARDetailCell.h"
#import "HuiKPlanV.h"
#import "ChuJJiLuV.h"
#import "HuanKPlanV.h"
#import "JieKXinXiV.h"

@implementation ARDetailCell
{
    HuiKPlanV *huiKuanV;
    HuanKPlanV *huankuanV;
    ChuJJiLuV *chujieV;
    JieKXinXiV *jiekuanV;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self UIConfig];
    }
    return self;
}
- (void)UIConfig{
    CGFloat height = SCREEN_HEIGHT-90-64;
    _scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,height)];
    _scroll.backgroundColor = UIColorFromRGB(ColorValueForBackView_Gray);
    _scroll.pagingEnabled = YES;
    _scroll.delegate = self;
    _scroll.showsHorizontalScrollIndicator = NO;
    _scroll.showsVerticalScrollIndicator = NO;
    [self.contentView addSubview:_scroll];
}

- (void)cellSetType:(NSInteger)page andBase:(NSInteger)base and:(UITableView *)baseTab{
    int cnt;
    if (base == 1) {
        if (page == 7||page == 6||page == 2||page == 700||page == 1||page == 0||page == 11||page == 12) {
            cnt = 3;
            _scroll.contentSize = CGSizeMake(SCREEN_WIDTH*cnt, 0);
        }else{
        cnt = 4;
            _scroll.contentSize = CGSizeMake(SCREEN_WIDTH*cnt, 0);
        }
    }else{
        cnt = 3;
        _scroll.contentSize = CGSizeMake(SCREEN_WIDTH*cnt, 0);
    }
    for (int i=0; i<cnt; i++) {
        if (cnt==4&&i==0) {
             huiKuanV = [[HuiKPlanV alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, _scroll.frame.size.height)];
            [huiKuanV baseTableView:baseTab];
            [_scroll addSubview:huiKuanV];
            continue;
        }
        if ((cnt==4&&i==1)||(cnt==3&&i==0)) {
            chujieV = [[ChuJJiLuV alloc] initWithFrame:CGRectMake(i*SCREEN_WIDTH, 0, SCREEN_WIDTH, _scroll.frame.size.height)];
            [chujieV baseTableView:baseTab];
            [_scroll addSubview:chujieV];
            continue;
        }
        if ((cnt==4&&i==2)||(cnt==3&&i==1)) {
            huankuanV = [[HuanKPlanV alloc] initWithFrame:CGRectMake(i*SCREEN_WIDTH, 0, SCREEN_WIDTH, _scroll.frame.size.height)];
            [huankuanV baseTableView:baseTab];
            huankuanV.baseType = base;
            [_scroll addSubview:huankuanV];
            continue;
        }
        if ((cnt==4&&i==3)||(cnt==3&&i==2)) {
            jiekuanV = [JieKXinXiV loadBundleNib];
            jiekuanV.frame = CGRectMake(i*SCREEN_WIDTH, 0, SCREEN_WIDTH, _scroll.frame.size.height);
            [_scroll addSubview:jiekuanV];
            continue;
        }
    }
}
#pragma mark - scrollDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger index = scrollView.contentOffset.x/SCREEN_WIDTH;
    [self.delegate cellScrollIndex:index];
}
- (void)cellScrollWithIndex:(NSInteger)index{
    _scroll.contentOffset = CGPointMake(index*SCREEN_WIDTH, 0);
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)setSubViewChuJieData:(id)data{
    [chujieV setChujieDataWith:data];
}
- (void)setSubViewHuanKuanData:(id)data{
    [huankuanV setHuanKuanData:data];
}
- (void)setSubViewHuiKuanData:(id)data{
    [huiKuanV setHuiKuanData:data];
}
- (void)setSubViewJieKuanData:(id)data{
    jiekuanV.moneyLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"借款金额：%@元",[data objectForKey:@"hopeamount"]] andlocation:5];
    if ([[NSString stringWithFormat:@"%@",[data objectForKey:@"priod"]] integerValue]==0) {
        jiekuanV.dateLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"借款期限：%@天",[data objectForKey:@"days"]] andlocation:5];
    }else{
        jiekuanV.dateLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"借款期限：%@个月",[data objectForKey:@"priod"]] andlocation:5];
    }
    
    jiekuanV.lilvLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"年化利率：%@%%",[data objectForKey:@"hoperate"]] andlocation:5];
    jiekuanV.reasonLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"借款理由：%@",[data objectForKey:@"summary"]] andlocation:5];
    jiekuanV.jiekuanfabutimeLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"借款发布时间：%@",[self timerStrWithTime:[data objectForKey:@"startTime"]]] andlocation:7];
    jiekuanV.youxiaoqiLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"借款有效期：%@天",[data objectForKey:@"validity"]] andlocation:6];
    jiekuanV.jixiLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"计息时间：%@",[self timerStrWithTime:[data objectForKey:@"interesttime"]]] andlocation:5];
    jiekuanV.huankuanfangshiLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"还款方式：%@",[data objectForKey:@"repaytype"]] andlocation:5];
    NSString *str;
    switch ([[data objectForKey:@"status"] integerValue]) {
            case 1:case 0:case 11:case 12:{str = @"借款中";}break;
            case 2:case 700:{str = @"已满标";}break;
            case 4:case 3:case 9:case 13:{str = @"还款中";}break;
            case 5:{str = @"已逾期";}break;
            case 7:case 6:{str = @"已流标";}break;
            case 8:case 10:{str = @"已还款";}break;
        default:
            break;
    }
    jiekuanV.statuLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"借款状态：%@",str] andlocation:5];

}



- (NSMutableAttributedString *)labelStr:(NSString *)baseStr andlocation:(NSInteger)location{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:baseStr];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, location)];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(location, str.length-location)];
    return str;
    
}
- (NSString *)timerStrWithTime:(NSString *)time{
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:[time doubleValue]/1000+28800];
    NSLog(@"date:%@",[detaildate description]);
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    return currentDateStr;
}

@end
